/*
 * Copyright 2013 s.randoll.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.digiway.rapidbreeze.server.infrastructure.persistence;

import de.digiway.rapidbreeze.server.TestUtil;
import de.digiway.rapidbreeze.server.infrastructure.objectstorage.ObjectStorage;
import de.digiway.rapidbreeze.server.model.config.ConfigurationChangeListener;
import de.digiway.rapidbreeze.server.model.config.ConfigurationChangeListener.ConfigurationChangeEvent;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import org.junit.After;
import org.junit.Test;
import static org.junit.Assert.*;
import static org.mockito.Mockito.*;
import org.junit.Before;
import org.mockito.ArgumentCaptor;

/**
 *
 * @author s.randoll
 */
public class ConfigurationRepositoryStorageIT {

    private ConfigurationRepositoryStorage configuration;
    private Path storagePath;
    private static final String KEY_1 = "Key1";
    private static final String KEY_2 = "Key2";
    private static final String VALUE_1 = "Value1";
    private static final String VALUE_2 = "Value2";

    @Before
    public void setup() throws IOException {
        storagePath = TestUtil.getNonExistingTemporaryPath();
        configuration = new ConfigurationRepositoryStorage(new ObjectStorage(storagePath));
        configuration.clearAll();
    }

    @After
    public void tearDown() throws IOException {
        TestUtil.deleteDirectoryRecursive(storagePath);
    }

    @Test
    public void testGetValue() {
        assertNull(configuration.getValue("bla"));
        configuration.setValue(KEY_1, VALUE_1);
        assertEquals(VALUE_1, configuration.getValue(KEY_1));
    }

    @Test
    public void testSetValue() {
        configuration.setValue(KEY_1, VALUE_1);
        configuration.setValue(KEY_2, VALUE_1);
        assertEquals(VALUE_1, configuration.getValue(KEY_1));
        assertEquals(VALUE_1, configuration.getValue(KEY_2));

        configuration.setValue(KEY_1, VALUE_2);
        configuration.setValue(KEY_2, VALUE_1);
        assertEquals(VALUE_2, configuration.getValue(KEY_1));
        assertEquals(VALUE_1, configuration.getValue(KEY_2));
    }

    @Test
    public void testSetValue_Path() {
        Path path = Paths.get("c:/");
        configuration.setValue(KEY_1, path);
        assertEquals(path, configuration.getValue(KEY_1));
    }

    @Test
    public void testListener() {
        ConfigurationChangeListener listener = mock(ConfigurationChangeListener.class);
        ArgumentCaptor<ConfigurationChangeEvent> eventCaptor = ArgumentCaptor.forClass(ConfigurationChangeEvent.class);

        configuration.addListener(listener);
        configuration.setValue(KEY_1, VALUE_1);
        configuration.setValue(KEY_1, VALUE_2);

        verify(listener, times(2)).onConfigurationChange(eventCaptor.capture());

        ConfigurationChangeEvent event = eventCaptor.getAllValues().get(0);
        assertEquals(KEY_1, event.getProperty());
        assertEquals(null, event.getOldValue());
        assertEquals(VALUE_1, event.getNewValue());

        event = eventCaptor.getAllValues().get(1);
        assertEquals(KEY_1, event.getProperty());
        assertEquals(VALUE_1, event.getOldValue());
        assertEquals(VALUE_2, event.getNewValue());

        // Remove listener again:
        configuration.removeListener(listener);
        configuration.setValue(KEY_1, VALUE_1);
        verifyZeroInteractions(listener);
    }

    @Test
    public void testGetAllConfigurations() {
        assertTrue(configuration.getAllConfigurations().isEmpty());

        configuration.setValue(KEY_1, VALUE_1);
        configuration.setValue(KEY_2, VALUE_2);

        assertEquals(2, configuration.getAllConfigurations().size());
        assertEquals(VALUE_1, configuration.getAllConfigurations().get(KEY_1));
        assertEquals(VALUE_2, configuration.getAllConfigurations().get(KEY_2));
    }
}
